package com.e2edour.biz.validator

import javax.validation.Validation
import org.apache.commons.collections.CollectionUtils

/**
  * <p>
  *
  * @author King 
  * @since 2017/1/17
  */
object ValidatorUtil {

  private val validator = Validation.buildDefaultValidatorFactory.getValidator

  @throws[IllegalArgumentException]
  def validate[T](obj: T) {
    val violations = validator.validate(obj)
    if (CollectionUtils.isNotEmpty(violations)) {
      import scala.collection.JavaConversions._
      for (constraintViolation <- violations) {
        throw new IllegalArgumentException(constraintViolation.getMessage)
      }
    }
  }
}
